package com.iaz.tech.tools.uml.file.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.iaz.tech.tools.common.utils.UtilTools;
import com.iaz.tech.tools.property.impl.PROPERTYRIGHT;
import com.iaz.tech.tools.uml.IUMLNeuronAble;
import com.iaz.tech.tools.uml.file.NeuronJSONTools;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronParameterStruct;

import lombok.Data;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode(callSuper=false)
@Data
public abstract class NeuronParameterStructLoader extends NeuronJSONTools{
    /**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(NeuronParameterStructLoader.class);

    public abstract NeuronParameterStruct toParameterStruct(IUMLNeuronAble clazz);

    /**
     * 解析JSON
     */
    @Override
    public void parseJSON(IUMLNeuronAble umlObj, JSONObject json) { 
        try {
            if (umlObj == null) {
                throw new Exception("实例化NeuronParameterLoader 异常， 入口参数umlObj 没有指定。");
            }
            if (json == null) {
                throw new Exception("实例化NeuronParameterLoader 异常， 入口参数json 没有指定。");
            }
            // if( !(umlObj instanceof NeuronParameterStruct)){
            //     throw new Exception("实例化NeuronParameterLoader 异常， 入口参数umlObj 不是NeuronParameterStruct。");
            // }
            NeuronParameterStruct parameterStruct = toParameterStruct(umlObj);

            if (json.containsKey("name")) {
                parameterStruct.setName(json.getString("name"));
            }
            if (json.containsKey("dataType")) {
                parameterStruct.setDataType(json.getString("dataType"));
            } 
            if (json.containsKey("right")) {
                String _right = json.getString("right");
                if (_right.equals("private")) {
                    parameterStruct.setRight(PROPERTYRIGHT.nprivate);
                } else if (_right.equals("public")) {
                    parameterStruct.setRight(PROPERTYRIGHT.npublic);
                } else if (_right.equals("protected")) {
                    parameterStruct.setRight(PROPERTYRIGHT.nprotected);
                }
            } 
            if (json.containsKey("static")) {
                String _static = json.getString("static");
                if (_static.equals("true")) {
                    parameterStruct.setSignStatic(true);
                } else if (_static.equals("false")) {
                    parameterStruct.setSignStatic(false);
                }
            }

            if (json.containsKey("final")) {
                String _final = json.getString("final");
                if (_final.equals("true")) {
                    parameterStruct.setSignFinal(true);
                } else if (_final.equals("false")) {
                    parameterStruct.setSignFinal(false);
                }
            } 

            if (json.containsKey("abstract")) {
                String _abstract = json.getString("abstract");
                if (_abstract.equals("true")) {
                    parameterStruct.setSignAbstract(true);
                } else if (_abstract.equals("false")) {
                    parameterStruct.setSignAbstract(false);
                }
            } 
        } catch(Exception e) { 
            logger.error("解析参数失败", e);
        } 
    }

    @Override
    public JSONObject toJSON(IUMLNeuronAble paramStruct) {
        JSONObject result = new JSONObject();
        try {
            if (paramStruct == null) {
                throw new Exception("要解析的UML对象为空"); 
            }
            
            NeuronParameterStruct param_struct =  toParameterStruct(paramStruct);
            if (UtilTools.IsNull( param_struct.getName())){
                throw new Exception("参数名称为空!");
            }
            result.put("name", param_struct.getName());
            if (UtilTools.IsNull( param_struct.getDataType())){
                throw new Exception("参数数据类型为空!");
            }
            result.put("dataType", param_struct.getDataType());
            
            if (param_struct.getRight() == null){
                throw new Exception("参数访问权限为空!");
            }

            if (param_struct.getRight() == PROPERTYRIGHT.nprivate){
                result.put("right", "private");
            } else if (param_struct.getRight() == PROPERTYRIGHT.npublic){
                result.put("right", "public");
            } else if (param_struct.getRight() == PROPERTYRIGHT.nprotected){
                result.put("right", "protected");
            }

            if (param_struct.isSignAbstract()) {
                result.put("abstract", "true");
            } else {
                result.put("abstract", "false");
            }
           
            if (param_struct.isSignFinal()) {
                result.put("final", "true");
            } else {
                result.put("final", "false");
            }

            if (param_struct.isSignStatic()) {
                result.put("static", "true");
            } else {
                result.put("static", "false");
            } 
        } catch (Exception e) {
            logger.error("解析JSON字符串失败", e); 
            result = new JSONObject();
        }
        return result;
    }
}
